/*-//GEN-BEGIN:copyright
 * ForBILE - Framework for Building Interactive Learning Environments
 * Computing Institute - Federal University of Alagoas - Brazil
 * Group of Otimization of the Web
 *
 * Copyright (C) 2007 Ig Bittencourt (ig.ibert@gmail.com)
 *		      Camila Nunes (camilapbnunes@hotmail.com)
 *	              Marcos Tadeu (zero.ufal@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *///GEN-END:copyright
package br.ufal.ic.utils;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * CriticalRegion.java
 *
 * <p>A full description of this class.
 *
 * @see SomeRelatedClass.
 *
 * @author <a href="mailto:ig.ibert@gmail.com">ig</a>.
 * @since 0.1
 * @version 0.1
 *
 */
public class CriticalRegion implements Remote {
    
    // Constants -----------------------------------------------------
    
    // Attributes ----------------------------------------------------
        
    private Map<Class, Entry> inputs;
    
    public  String            name;    
    
    // Static --------------------------------------------------------
       
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(CriticalRegion.class.getName());
    }
    
    // Constructors --------------------------------------------------
    
    /**
     * Creates a new instance of CriticalRegion.
     *
     * @param The name of the critical regio.
     */
    public CriticalRegion(final String name) throws RemoteException {
        this.name   = name;        
        this.inputs = new HashMap<Class, Entry>();
    }
    
    // Public --------------------------------------------------------            
    
    /**
     * Write in the critical region if it does not exist. 
     *
     * @param template The content will be verified.
     */
    public void writeIfNotExist(final Entry template) throws RemoteException {
        if (inputs.containsKey(template.getClass())) {
            Thread t = new Thread();

            while (inputs.containsKey(template.getClass())) {
                try {
                    t.sleep(600);
                } catch (InterruptedException ex) {
                    logger.throwing(getClass().getName(), "writeIfNotExist", ex);
                }
            }
        }

        inputs.put(template.getClass(), template);
    }
    
    /**
     * Write in the critical region. 
     *
     * @param template The content.
     */
    public void write(final Entry template) throws RemoteException {
        inputs.put(template.getClass(), template);
    }
    
    /**
     * Read of the critical region. 
     *
     * @param template The content.
     * 
     * @return The specified entry.
     */
    public Entry read(final Entry template) throws RemoteException {
        return inputs.get(template.getClass());
    }
    
    /**
     * Retrieve the content. 
     *
     * @param template 
     *
     * @return the entry.
     */
    public Entry retrieve(final Entry template) throws RemoteException {
        return inputs.remove(template.getClass());
    }
    
    /**
     * Retrieve the content if exists. 
     *
     * @param template The content
     *
     * @return The entry if exists.
     */
    public Entry retrieveIfExist(final Entry template) throws RemoteException {
        if (!inputs.containsKey(template.getClass())) {
            Thread t = new Thread();
            
            while (!inputs.containsKey(template.getClass())) {
                try {
                    t.sleep(600);
                } catch (InterruptedException ex) {
                    logger.throwing(getClass().getName(), "retrieveIfExist", ex);
                }
            }
        }
        
        return inputs.remove(template.getClass());
    }
    
    /**
     * Override. 
     *
     * @return 
     */
    public String toString() {
        return inputs.toString();
    }
    
    // Z implementation ----------------------------------------------
    
    // Y overrides ---------------------------------------------------
    
    // Package protected ---------------------------------------------
    
    // Protected -----------------------------------------------------
    
    // Private -------------------------------------------------------
    
    // Inner classes -------------------------------------------------
    
}
